/*
 * Created on Feb 13, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.query.compare;

import java.lang.reflect.Constructor;

import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.ProfileListenerBase;
import edu.mit.simile.longwell.query.QueryException;

/**
 * @author dfhuynh
 */
public class ComparatorManager extends ProfileListenerBase {
	final protected Profile m_profile;

	public ComparatorManager(Profile profile) {
		m_profile = profile;
	}

	public IComparator getComparator(String name, String parameter) throws QueryException {
		if (name == null) {
			name = ComparatorBase.class.getName();
		}
		if (parameter == null) {
			parameter = "";
		}
		
		try {
			Constructor constructor = 
				Class.forName(name).getConstructor(
					new Class[] { Profile.class, String.class });
			
			return (IComparator) constructor.newInstance(
					new Object[] { m_profile, parameter });
		} catch (Exception e) {
			throw new QueryException(
				"Failed to create comparator of name " + name, e);
		}
	}
}
